<?php
/****************************************************************************
 ** file: add-locations.php
 **
 ** handles the add locations form
 ***************************************************************************/

 
function add_this_addy($fields,$values,$theaddress) {
    global $wpdb;
    $fields=substr($fields, 0, strlen($fields)-1);
    $values=substr($values, 0, strlen($values)-1);  
    $wpdb->query("INSERT into ". $wpdb->prefix . "store_locator ($fields) VALUES ($values)");		
    	dk_do_geocoding($theaddress);
    //record user id	
	update_option( 'dk_locator_user_record' . $wpdb->insert_id , dk_get_e_member_id() );
}




/****************************************************************************
 ***************************************************************************/

global $wpdb, $sl_upload_path, $sl_path;

print "<div class='wrap'>
            <div id='icon-add-locations' class='icon32'><br/></div>
            <h2>".
            __('Directory Listing - Add Locations', SLPLUS_PREFIX).
            "</h2>";
            
initialize_variables();

//-------------------------
// Navbar Section
//-------------------------    



//Inserting addresses by manual input
//
$notpca = isset($_GET['mode']) ? ($_GET['mode']!="pca") : true;
if ( isset($_POST['sl_store']) && $_POST['sl_store'] && $notpca ) {
    $fieldList = '';
    $valueList = '';
    foreach ($_POST as $key=>$value) {
        if (ereg("sl_", $key)) {
            $fieldList.="$key,";
            $value=comma($value);
            $valueList.="\"".stripslashes($value)."\",";
        }
    }

    $this_addy = $_POST['sl_address'].', '.
              $_POST['sl_city'].', '.$_POST['sl_state'].' '.
              $_POST['sl_zip'];
    add_this_addy($fieldList,$valueList,$this_addy);
    print "<div class='updated fade'>".
            $_POST['sl_store'] ." " .
            __("Added Succesfully",SLPLUS_PREFIX) . '.</div>';
            
/** Bulk Upload
 **/
} elseif ( isset($_FILES['csvfile']['name']) && 
       ($_FILES['csvfile']['name']!='')  &&
        ($_FILES['csvfile']['size'] > 0)
    ) { 

    if  (function_exists('custom_upload_mimes')) {
        add_filter('upload_mimes', 'custom_upload_mimes');
    }   

    // Get the type of the uploaded file. This is returned as "type/extension"
    $arr_file_type = wp_check_filetype(basename($_FILES['csvfile']['name']));
    if ($arr_file_type['type'] == 'text/csv') {

                // Save the file to disk
                //
                $updir = wp_upload_dir();
                $updir = $updir['basedir'].'/slplus_csv';
                if(!is_dir($updir)) {
                    mkdir($updir,0755);
                }
                if (move_uploaded_file($_FILES['csvfile']['tmp_name'],
                        $updir.'/'.$_FILES['csvfile']['name'])) {
                        $reccount = 0;
                             
                        $adle_setting = ini_get('auto_detect_line_endings');
                        ini_set('auto_detect_line_endings', true);                         
                        if (($handle = fopen($updir.'/'.$_FILES['csvfile']['name'], "r")) !== FALSE) {
                            $fldNames = array('sl_store','sl_address','sl_address2','sl_city','sl_state',
                                            'sl_zip','sl_country','sl_tags','sl_description','sl_url',
                                            'sl_hours','sl_phone','sl_email');
                            $maxcols = count($fldNames);
                            while (($data = fgetcsv($handle)) !== FALSE) {
                                $num = count($data);
                                if ($num <= $maxcols) {
                                    $fieldList = '';
                                    $valueList = '';
                                    $this_addy = '';
                                    for ($fldno=0; $fldno < $num; $fldno++) {
                                        $fieldList.=$fldNames[$fldno].',';
                                        $valueList.="\"".stripslashes(comma($data[$fldno]))."\",";
                                        if (($fldno>=1) && ($fldno<=6)) {
                                            $this_addy .= $data[$fldno] . ', ';
                                        }
                                    }
                                    $this_addy = substr($this_addy, 0, strlen($this_addy)-2);
                                    add_this_addy($fieldList,$valueList,$this_addy);
                                    sleep(0.5);
                                    $reccount++;
                                } else {
                                     print "<div class='updated fade'>".
                                        __('The CSV file has too many fields.',
                                            SLPLUS_PREFIX
                                            );
                                     print ' ';
                                     printf(__('Got %d expected less than %d.', SLPLUS_PREFIX),
                                        $num,$maxcols);
                                     print '</div>';                                    
                                }                                    
                            }
                            fclose($handle);
                        }
                        ini_set('auto_detect_line_endings', $adle_setting);                         
                        

                        if ($reccount > 0) {
                            print "<div class='updated fade'>".
                                    sprintf("%d",$reccount) ." " .
                                    __("locations added succesfully.",SLPLUS_PREFIX) . '</div>';
                        }                                

                // Could not save
                } else {
                        print "<div class='updated fade'>".
                        __("File could not be saved, check the plugin directory permissions:",SLPLUS_PREFIX) .
                            "<br/>" . $updir.

                '.</div>';  
        }
                        
        // Not CSV Format Warning           
    } else {
        print "<div class='updated fade'>".
            __("Uploaded file needs to be in CSV format.",SLPLUS_PREFIX) . 
            " Type was " . $arr_file_type['type'] . 
            '.</div>';  
    }
}

    
$base=get_option('siteurl');

// Show the manual location entry form
// execute_and_output_template('add_locations.php');
?>


<?php 
global $text_domain, $sl_upload_path, $slplus_plugin;
?>
<table cellpadding='10px' cellspacing='0' style='width:100%' class='manual_add_table'>
    <tr>
        <td style='padding-top:0px;' valign='top'>
            <form name='manualAddForm' method=post enctype="multipart/form-data">
                <table cellpadding='0' class='widefat'>
                <thead><tr><th><?php _e("Enter An Address", $text_domain);?></th></tr></thead>
                <tr><td>
                    <div class="add_location_form">
                    <label  for='sl_store'><?php _e('Name of Location', $text_domain);?></label>
                    <input name='sl_store'><br/>
        
                    <label  for='sl_address'><?php _e('Street - Line 1', $text_domain);?></label>
                    <input name='sl_address'><br/>
        
                    <label  for='sl_address2'><?php _e('Street - Line 2', $text_domain);?></label>
                    <input name='sl_address2'><br/>
        
                    <label  for='sl_city'><?php _e('City, State, ZIP', $text_domain);?></label>
                    <input name='sl_city'   style='width: 21.4em; margin-right: 1em;'>
                    <input name='sl_state'  style='width: 7em; margin-right: 1em;'>
                    <input name='sl_zip'    style='width: 7em;'>
                    <br/>
        
                    <label  for='sl_country'><?php _e('Country', $text_domain);?></label>
                    <input name='sl_country'><br/>
                    <br/>
                    <input type='submit' value='<?php _e("Add Location", $text_domain);?>' class='button-primary'>
                    </div>
        </td></tr>
        <thead><tr><th><?php _e("Additional Information", $text_domain);?></th></tr></thead>
        <tr><td><div class="add_location_form">
		    <label for='sl_description'><?php _e("Description", $text_domain);?></label>
            <textarea name='sl_description' rows='5'></textarea><br/>
            
			<!--add tags auto -->
            <input name='sl_tags' value = "<?php echo dk_get_e_member() ;?>" style = "display:none"><br/>
            <label for='sl_url'><?php _e("URL", $text_domain);?></label>
            <input name='sl_url'><br/>
            
            <label for='sl_email'><?php _e("email", $text_domain);?></label>
            <input name='sl_email'><br/>
            
            <label for='sl_hours'><?php _e("Hours", $text_domain);?></label>
            <input name='sl_hours'><br/>
            
            <label for='sl_phone'><?php _e("Phone", $text_domain);?></label>
            <input name='sl_phone'><br/>
            
            <label for='sl_image'><?php _e("Image URL (shown with location)", $text_domain);?></label>
            <input name='sl_image'><br/>
            <br/>
            
            <input type='submit' value='<?php _e("Add Location", $text_domain);?>' class='button-primary'>
            </div>
	</div></td>
		</tr>

          		
	</table>
	</form>
	</td>
    </tr>
</table>
</div>


